/* rcctwba.p - Container Workbench Record Sort Routine                        */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                        */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Revision: 1.3.1.3 $                                                         */
/*V8:ConvertMode=NoConvert                                                    */
/*                  LAST MODIFIED: 04/14/95           BY: GWM *J049*          */
/*                  LAST MODIFIED: 04/14/95           BY: LAL *J139*          */
/* REVISION: 8.6    LAST MODIFIED: 05/20/98  BY: *K1Q4*  Alfred Tan           */
/* REVISION: 9.1    LAST MODIFIED: 08/12/00  BY: *N0KP*  Mark Brown           */
/* Revision: 1.3.1.2    BY: Jean Miller  DATE: 03/22/01 ECO: *P008*           */
/* $Revision: 1.3.1.3 $    BY: Abbas Hirkani    DATE: 05/05/01 ECO: *M16P*        */
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

{mfdeclre.i}

/* INPUT PARAMETERS */
define input  parameter abs_recid as recid no-undo.
define input  parameter tmp_prefix as character no-undo.
define output parameter return_code as integer no-undo.

/* LOCAL VARIABLES */
define variable ship_from like abs_shipfrom no-undo.
define variable cont_level as integer no-undo.
define variable next_nbr as integer no-undo.

/* BUFFERS */
define buffer abs_temp for abs_mstr.

/*****************************/
/* POSSIBLE RETURN CODES     */
/* 0 = NORMAL                */
/* 1 = MAX LEVELS EXCEEDED   */
/* 2 = RECORD ALREADY LOCKED */
/* 3 = MAX RECORDS EXCEEDED  */
/* 4 = NO RECORDS TO SORT    */
/*****************************/

main:
do:

   return_code = 0.

   find abs_mstr where recid(abs_mstr) = abs_recid
   exclusive-lock no-wait no-error.

   if locked(abs_mstr) then do:
      {pxmsg.i &MSGNUM=7422 &ERRORLEVEL=4}
      /* RECORD LOCKED - PLEASE TRY AGAIN LATER */
      return_code = 2.
      leave main.
   end.

   /* NO RECORDS TO SORT */
   if not available abs_mstr then do:
      return_code = 4.
      leave main.
   end.

   assign
      ship_from = abs_mstr.abs_shipfrom.

   /* FIND THE HIGHEST LEVEL RECORD */
   parent_loop:
   repeat:

      /* IF A NON TEMP NUMBER THEN SAVE */
      if index(abs_mstr.abs_shipto,"::") = 0 then
         abs_mstr.abs__qad05 = abs_mstr.abs_shipto.

      assign
         abs_mstr.abs_shipto = tmp_prefix.

      find abs_temp where
           abs_temp.abs_shipfrom = abs_mstr.abs_shipfrom and
           abs_temp.abs_id = abs_mstr.abs_par_id
      no-lock no-error.

      /* NO MORE PARENT RECORDS                    */
      /* SAVE THE RECID OF abs_mstr BEFORE LEAVING */
      /* DUE TO ORACLE REQUIREMENTS.               */
      if not available abs_temp
      then do:
         abs_recid = recid(abs_mstr).
         leave parent_loop.
      end. /* IF NOT AVAILABLE abs_temp */

      /* FIND AND LOCK THE PARENT RECORD */
      else
      find abs_mstr where recid(abs_mstr) = recid(abs_temp)
      exclusive-lock no-wait no-error.

      if locked(abs_mstr) then do:
         {pxmsg.i &MSGNUM=7422 &ERRORLEVEL=4}
         /* RECORD LOCKED - PLEASE TRY AGAIN LATER */
         return_code = 2.
         leave main.
      end.

   end. /* parent_loop */

   /* FIND THE abs_mstr USING THE ABOVE SAVED RECID     */
   for first abs_mstr
      fields (abs_par_id)
      where recid(abs_mstr) = abs_recid
      no-lock:
   end. /* FOR FIRST abs_mstr */

   /* NOW HAVE THE HIGHEST LEVEL ABS_MSTR IN THIS CHAIN */
   /* SORT FOR ITEMS FIRST */
   for each abs_temp where
            abs_temp.abs_shipfrom = ship_from
        and abs_temp.abs_par_id = abs_mstr.abs_par_id
        and abs_temp.abs_shipto begins tmp_prefix
        and abs_temp.abs_id begins "I"
   no-lock:

      cont_level = 0.

      /* SET THE SORT ORDER AND LEVEL FOR THIS AND ALL CHILD RECORDS */
      {gprun.i ""rcctwba1.p""
         "(input recid(abs_temp),
           input cont_level,
           input tmp_prefix,
           input-output next_nbr)"}

      if next_nbr = -1 then do:
         {pxmsg.i &MSGNUM=758 &ERRORLEVEL=4}
         /* MAXIMUM NUMBER OF LEVELS EXCEEDED */
         return_code = 1.
         leave main.
      end.

      if next_nbr = -2 then do:
         {pxmsg.i &MSGNUM=7422 &ERRORLEVEL=4}
         /* RECORD LOCKED - TRY AGAIN LATER */
         return_code = 2.
         leave main.
      end.

      next_nbr = next_nbr + 1.

      if next_nbr > 9999 then do:
         {pxmsg.i &MSGNUM=759 &ERRORLEVEL=4}
         /* MAXIMUM NUMBER OF RECORDS EXCEEDED */
         return_code = 3.
         leave main.
      end.

   end. /* FOR EACH ABS_TEMP */

   /* NOW SORT FOR CONTAINERS */
   for each abs_temp where
            abs_temp.abs_shipfrom = ship_from
        and abs_temp.abs_par_id = abs_mstr.abs_par_id
        and abs_temp.abs_shipto begins tmp_prefix
        and abs_temp.abs_id <> ""
        and not abs_temp.abs_id begins "I"
   no-lock:

      cont_level = 0.

      /* SET THE SORT ORDER AND LEVEL FOR THIS AND ALL CHILD RECORDS */
      {gprun.i ""rcctwba1.p""
         "(input recid(abs_temp),
           input cont_level,
           input tmp_prefix,
           input-output next_nbr)"}

      if next_nbr = -1 then do:
         {pxmsg.i &MSGNUM=758 &ERRORLEVEL=4}
         /* MAXIMUM NUMBER OF LEVELS EXCEEDED */
         return_code = 1.
         leave main.
      end.

      if next_nbr = -2 then do:
         {pxmsg.i &MSGNUM=7422 &ERRORLEVEL=4}
         /* RECORD LOCKED - TRY AGAIN LATER */
         return_code = 2.
         leave main.
      end.

      next_nbr = next_nbr + 1.

      if next_nbr > 9999 then do:
         {pxmsg.i &MSGNUM=759 &ERRORLEVEL=4}
         /* MAXIMUM NUMBER OF RECORDS EXCEEDED */
         return_code = 3.
         leave main.
      end.

   end. /* FOR EACH ABS_TEMP */

end. /* MAIN */
